package com.wlk.controller;

import com.wlk.pojo.Teachers;
import com.wlk.service.TeachersService;
import com.wlk.utils.Result;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;

@Controller
@RequestMapping("/teachers")
public class TeachersController {
    @Resource
    private TeachersService teachersService;

    /*
    * 展示教师的数据
    * */
    @GetMapping("/teacher")
    public String teacher(){
        return "teacher";
    }

    /*
    * 获取教师信息列表（分页查询）
    * */
    @ResponseBody
    @GetMapping("/{page}/{size}")
    public Result getTeachersList(@PathVariable("page") int page, @PathVariable("size") int size){
        return teachersService.getTeachersList(page, size);
    }

    /*
    * 跳转添加教师页面
    * */
    @GetMapping("/addTeacher")
    public String add(){
        return "add_teacher";
    }


    @PostMapping("/addTeacher")
    @ResponseBody
    public String addTeacher(@RequestBody Teachers teachers){
        Result result = teachersService.addTeacher(teachers);
        if(result.getCode().equals(200)){
            return "redirect:/teachers/teacher";
        }else{
            return "add_teacher";
        }
    }

    @DeleteMapping("/deleteTeacher/{id}")
    @ResponseBody
    public Result deleteTeacher(@PathVariable("id") int id){
        return teachersService.deleteTeacher(id);
    }



}
